<?php
/**
 * Add feed procedure
 *
 * @author University of London Computer Centre
 * @version 1.4.0 - Release Candidate 2
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 */
// globals
global $ArchivePress;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php';

// CHECK FOR MESSAGES
$message = "";
$pClass = "";

// added
if(array_key_exists("added", $_GET)){
    $message = __("Feed Added", ARCHIVEPRESS);
}

// ERROR
if(array_key_exists("error", $_GET)){
    $message = $ArchivePress->cleanOutput($_GET["error"]);
    $pClass = "class='arpr_alert'";
}

if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p '.$pClass.'>
                        '.$message.'<br />
                    </p>
                </div>';
}

// LOADING gif
echo '<div id="arpr_loadinggif"><img src="../wp-content/plugins/archivepress/images/ajax-loader.gif" alt="" /></div>';

echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("Add Feed", ARCHIVEPRESS).'</h2>
        '.$message.'
        <form method="post" action="admin.php?page=archivepress/pages/add-feed.php" enctype="multipart/form-data">
            <input type="hidden" name="back" value="add-feed" />
            <input type="hidden" name="' . WP_ARCHIVEPRESS_PREFIX . 'action" value="addfeed" />';

// security
wp_nonce_field(WP_ARCHIVEPRESS_PREFIX . 'addfeed');

// Feed Settings
// Try to Load the saved user values (this because an error occoured)
// If there are not old values, then uses the DEFAULT ones
$name               = $ArchivePress->getTmpOption("name");

$url                = $ArchivePress->getTmpOption("url");
$url ? "" : $url = "http://";

$protocol           = $ArchivePress->getTmpOption("protocol");
$protocol ? "" : $protocol = get_option(WP_ARCHIVEPRESS_PREFIX . "defFeedProtocol");

$active             = $ArchivePress->getTmpOption("active");
$active ? "" : $active = get_option(WP_ARCHIVEPRESS_PREFIX . "justAddedIsActive");

$interval           = $ArchivePress->getTmpOption("interval");
$interval ? "" : $interval = get_option(WP_ARCHIVEPRESS_PREFIX . "defFeedFetchInt");

$comments           = $ArchivePress->getTmpOption("comments");
$comments ? "" : $comments = get_option(WP_ARCHIVEPRESS_PREFIX . "postFetchComments");

$followPermaLink    = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "followPermaLink");
$followPermaLink ? "" : $followPermaLink = get_option(WP_ARCHIVEPRESS_PREFIX . "followPermaLink");

$embedMedia         = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "embedMedia");
$embedMedia ? "" : $embedMedia = get_option(WP_ARCHIVEPRESS_PREFIX . "embedMedia");

$defCommentsStatus  = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus");
$defCommentsStatus ? "" : $defCommentsStatus = get_option(WP_ARCHIVEPRESS_PREFIX . "defCommentsStatus");

$tryToFindExcerpt   = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt");
$tryToFindExcerpt ? "" : $tryToFindExcerpt = get_option(WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt");

$defPostStatus      = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "defPostStatus");
$defPostStatus ? "" : $defPostStatus = get_option(WP_ARCHIVEPRESS_PREFIX . "defPostStatus");

// more than one feed links (error)
$feedLinks          = $ArchivePress->getTmpOption(WP_ARCHIVEPRESS_PREFIX . "feedlinks");
$feedLinksSelect    = "";
if($feedLinks != "") {
    $feedLinks = unserialize($feedLinks);
    $feedLinksSelect    .= '<tr>
                                <th scope="row">'.__("Feed Urls", ARCHIVEPRESS).'</th>
                                <td>
                                    <fieldset><legend class="screen-reader-text"><span>'.__("Feed Urls", ARCHIVEPRESS).'</span></legend>';
    for($i = 0; $i < count($feedLinks); $i++) {
        $feedLinksSelect    .= '
                                        <label title="'.$feedLinks[$i].'"><input type="radio" checked="checked" value="'.$feedLinks[$i].'" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedlink" /> '.$feedLinks[$i].'</label><br />';
    }
    $feedLinksSelect    .= '
                                    </fieldset>
                                </td>
                                </tr>';

}

echo'
            <h3>'.__("Basic Options", ARCHIVEPRESS).'</h3>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedname">'.__("Feed Name").'</label></th>
                    <td>
                        <input type="text" class="regular-text" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedname" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedname" value="'.$name.'" />
                    </td>
                </tr>';

                if($feedLinksSelect == "") {

echo'
                <tr valign="top">
                    <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl">'.__("Feed Url").'</label></th>
                    <td>
                        <input type="text" class="regular-text" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedurl" value="'.$url.'" />
                        <span class="description">'.__("This can be an Atom, RSS or blog url.", ARCHIVEPRESS).'</span>
                    </td>
                </tr>

                <tr valign="top">
                    <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedfile">'.__("Whole Blog File").'</label></th>
                    <td>
                        <input type="file" class="regular-text" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedfile" name="' . WP_ARCHIVEPRESS_PREFIX . 'feedfile" value="" />
                        <span class="description">'.__("You can upload an XML file containing ALL the feeds for a specific Blog.", ARCHIVEPRESS).'</span><br />
                        <span class="description arpr_tip">'.__("The XML formats currently supported are RSS and Atom (WP and Blogger coming soon).", ARCHIVEPRESS).'</span>
                    </td>
                </tr>';

                } else {
                    echo $feedLinksSelect;
                }

echo '

            </table>

            <br />

            <h3 id="arpr_openadvanced">'.__("Advanced Settings", ARCHIVEPRESS).'</h3>
            <div id="arpr_advanced">
                <p>'.__("You don't need to modify these settings: you can just keep the default configuration.", ARCHIVEPRESS).'</p>
                <p class="arpr_alert">'.__("Modify these settings may affect your server performances, so use change these options only if you know exactly what you are doing.", ARCHIVEPRESS).'</p>

                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedprotocol">'.__("Feed Protocol").'</label></th>
                        <td>
                            <select  name="' . WP_ARCHIVEPRESS_PREFIX . 'feedprotocol" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedprotocol">
                                <option value="'.ArchivePressOptions::FEED_PROTOCOL_ATOM.'"'; if(ArchivePressOptions::FEED_PROTOCOL_ATOM == $protocol) echo 'selected="selected"'; echo' >'.__("Atom", ARCHIVEPRESS).'</option>
                                <option value="'.ArchivePressOptions::FEED_PROTOCOL_RSS.'"'; if(ArchivePressOptions::FEED_PROTOCOL_RSS == $protocol) echo 'selected="selected"'; echo' >'.__("Rss", ARCHIVEPRESS).'</option>
                            </select>
                            <span class="description">'.__("Each Blog can have more than one Feed (RSS, Atom, ...), so you can decide which one use as default.", ARCHIVEPRESS).'</span>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">'.__("Activate Feed").'</th>
                        <td>
                            <fieldset>
                                <legend class="screen-reader-text">
                                    <span>'.__("Activate Feed").'</span>
                                </legend>
                                <label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedactive">
                                    <input name="' . WP_ARCHIVEPRESS_PREFIX . 'feedactive" type="checkbox" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedactive" value="yes" '; if($active == "yes") echo 'checked="checked"'; echo' />
                                    '.__("check to make it active").'
                                </label>
                            </fieldset>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedinterval">'.__("Fetch Interval").'</label></th>
                        <td>
                            <select  name="' . WP_ARCHIVEPRESS_PREFIX . 'feedinterval" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedinterval">';
                            // get intervals
                            $intervals = array(
                                                    ArchivePressOptions::FETCH_INTERVAL_HOURLY  => __("Every Hour", ARCHIVEPRESS),
                                                    ArchivePressOptions::FETCH_INTERVAL_FOURTAD => __("Four Times a Day", ARCHIVEPRESS),
                                                    ArchivePressOptions::FETCH_INTERVAL_TID     => __("Three Times a Day", ARCHIVEPRESS),
                                                    ArchivePressOptions::FETCH_INTERVAL_BID     => __("Two Times a Day", ARCHIVEPRESS),
                                                    ArchivePressOptions::FETCH_INTERVAL_DAILY   => __("Every Day", ARCHIVEPRESS)
                                               );

                            foreach($intervals as $value => $text) {
                                echo '
                                <option value="'.$value.'"'; if($value == $interval) echo 'selected="selected"'; echo' >'.$ArchivePress->cleanOutput($text).'</option>
                                ';
                            }
    echo'
                            </select>
                            <span class="description">'.__("How often the Feed has to be fetched.", ARCHIVEPRESS).'</span> <br /><span class="description arpr_tip">'.__("Too often generate more trafic on your blog, but too infrequently could let your server use too many resources.", ARCHIVEPRESS).'</span>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">'.__("Fetch Comments").'</th>
                        <td>
                            <fieldset>
                                <legend class="screen-reader-text">
                                    <span>'.__("Fetch Comments").'</span>
                                </legend>
                                <label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedcomments">
                                    <input name="' . WP_ARCHIVEPRESS_PREFIX . 'feedcomments" type="checkbox" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedcomments" value="yes" '; if($comments == "yes") echo 'checked="checked"'; echo' />
                                    '.__("check to fetch also comments for this Feed").'
                                </label>
                            </fieldset>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">'.__("Follow PermaLinks").'</th>
                        <td>
                            <fieldset>
                                <legend class="screen-reader-text">
                                    <span>'.__("Follow PermaLinks").'</span>
                                </legend>
                                <label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedpermalink">
                                    <input name="' . WP_ARCHIVEPRESS_PREFIX . 'feedpermalink" type="checkbox" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedpermalink" value="yes" '; if($followPermaLink == "yes") echo 'checked="checked"'; echo' />
                                    '.__("check to try to download and save the ORIGINAL content from the Blog instead of using the Feed one").'
                                </label>
                            </fieldset>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">'.__("Embed Media").'</th>
                        <td>
                            <fieldset>
                                <legend class="screen-reader-text">
                                    <span>'.__("Embed Media").'</span>
                                </legend>
                                <label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedmedia">
                                    <input name="' . WP_ARCHIVEPRESS_PREFIX . 'feedmedia" type="checkbox" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedmedia" value="yes" '; if($embedMedia == "yes") echo 'checked="checked"'; echo' />
                                    '.__("check to download and store the post images instead of use links to the original files").'
                                </label>
                            </fieldset>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus">'.__("Post Status").'</label></th>
                        <td>
                            <select  name="' . WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedpoststatus">
                                <option value="'.ArchivePressOptions::POST_DRAFT.'"'; if(ArchivePressOptions::POST_DRAFT == $defPostStatus) echo 'selected="selected"'; echo' >'.__("Draft", ARCHIVEPRESS).'</option>
                                <option value="'.ArchivePressOptions::POST_PENDING.'"'; if(ArchivePressOptions::POST_PENDING == $defPostStatus) echo 'selected="selected"'; echo' >'.__("Pending", ARCHIVEPRESS).'</option>
                                <option value="'.ArchivePressOptions::POST_PUBLISH.'"'; if(ArchivePressOptions::POST_PUBLISH == $defPostStatus) echo 'selected="selected"'; echo' >'.__("Published", ARCHIVEPRESS).'</option>
                            </select>
                            <span class="description">'.__("Indicates the status for any post added (for this Feed)", ARCHIVEPRESS).'</span>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">'.__("Excerpt").'</th>
                        <td>
                            <fieldset>
                                <legend class="screen-reader-text">
                                    <span>'.__("Excerpt").'</span>
                                </legend>
                                <label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt">
                                    <input name="' . WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt" type="checkbox" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedexcerpt" value="yes" '; if($tryToFindExcerpt == "yes") echo 'checked="checked"'; echo' />
                                    '.__("check if you want the plugin will create excerpts for the posts added").'
                                </label>
                            </fieldset>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><label for="' . WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus">'.__("Comment Status").'</label></th>
                        <td>
                            <select  name="' . WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus" id="' . WP_ARCHIVEPRESS_PREFIX . 'feedcommentstatus">
                                <option value="'.ArchivePressOptions::COMMENTS_CLOSED.'"'; if(ArchivePressOptions::COMMENTS_CLOSED == $defCommentsStatus) echo 'selected="selected"'; echo' >'.__("Closed", ARCHIVEPRESS).'</option>
                                <option value="'.ArchivePressOptions::COMMENTS_OPEN.'"'; if(ArchivePressOptions::COMMENTS_OPEN == $defCommentsStatus) echo 'selected="selected"'; echo' >'.__("Open", ARCHIVEPRESS).'</option>
                            </select>
                            <span class="description">'.__("Indicates if to allow comments for the posts added.", ARCHIVEPRESS).'</span>
                        </td>
                    </tr>

                </table>

            </div>

            <br />

            <p class="submit">
            <input type="submit" id="arpr_add_feed_sub" class="button-primary" value="'.__('Add Feed', ARCHIVEPRESS).'" />
            </p>';

echo '
        </form>
     </div>';
?>